package com.example.idachuappone.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;

import android.os.Environment;

public class FileUtil {
	private static final String PIC_CACHE_DIR = "/AppPicCache/lcafinalCache";
	private static final String USER_ICON_DIR = "/LcUserIcon";
	private static final String XP_CACHE_DIR = "/LoveChefCache";

	private static File getRootDir() {
		return Environment.getExternalStorageDirectory();
	}

	public static Boolean SDCardExist() {
		return Boolean.valueOf(Environment.getExternalStorageState().equals("mounted"));
	}

	public static File getCacheDir() {
		File file = new File(getRootDir(), XP_CACHE_DIR);
		createDir(file);
		return file;
	}

	private static void createDir(File file) {
		if (!file.exists()) {
			file.mkdirs();
		}
	}

	public static File getUserHeadIcDir() {
		File file = new File(getCacheDir(), USER_ICON_DIR);
		createDir(file);
		return file;
	}

	public static File getUserCapIcDir() {
		File file = new File(getCacheDir(), USER_ICON_DIR);
		createDir(file);
		return file;
	}

	public static String getAppPicCacheDir() {
		File file = new File(getCacheDir(), PIC_CACHE_DIR);
		createDir(file);
		return file.getAbsolutePath();
	}

	public static String getBitmapUtilCacheDir() {
		File file = new File(getCacheDir(), "/bmcache");
		createDir(file);
		return file.getAbsolutePath();
	}

	public static long getFileSize(File file) throws Exception {
		long size = 0;
		File[] flist = file.listFiles();
		for (int i = 0; i < flist.length; i++) {
			if (flist[i].isDirectory()) {
				size += getFileSize(flist[i]);
			} else {
				size += flist[i].length();
			}
		}
		return size;
	}

	public static String FormetFileSize(long size) {
		DecimalFormat df = new DecimalFormat("#.00");
		long mb = 1024 * 1024;
		long gb = 1024 * mb;
		if (size == 0) {
			return "0KB";
		}
		if (size < 1024) {
			return "<1KB";
		}
		if (size < mb) {
			return df.format(((double) size) / ((double) 1024)) + "K";
		}
		return size < gb ? df.format(((double) size) / ((double) mb)) + "M" : df
				.format(((double) size) / ((double) gb)) + "G";
	}

	public static void delAllFile(File file) {
		if (!file.exists()) {
			return;
		}
		if (file.isFile()) {
			file.delete();
		} else if (file.isDirectory()) {
			File[] childFile = file.listFiles();
			if (childFile == null || childFile.length == 0) {
				file.delete();
				return;
			}
			for (File f : childFile) {
				delAllFile(f);
			}
			file.delete();
		}
	}

	public static String getString(InputStream inputStream) {
		InputStreamReader inputStreamReader = null;
		try {
			inputStreamReader = new InputStreamReader(inputStream, "gbk");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		BufferedReader reader = new BufferedReader(inputStreamReader);
		StringBuffer sb = new StringBuffer("");
		while (true) {
			try {
				String line = reader.readLine();
				if (line != null) {
					sb.append(line);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			break;
		}

		try {
			reader.close();
			inputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
	}
}
